package cn.yunlang.zhangZhongFei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.yunlang.zhangZhongFei.entity.Company;
import cn.yunlang.zhangZhongFei.mapper.CompanyMapper;
import cn.yunlang.zhangZhongFei.service.CompanyService;
import cn.yunlang.zhangZhongFei.vo.response.R;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 公司信息表 服务实现类
 *
 * @author ·
 * @since 2025-05-27 09:54:49
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company>
    implements CompanyService {

  public R<IPage<Company>> pageCompany(Integer pageNo, Integer pageSize, String companyName) {
    if (pageNo == null || pageSize == null) {
      return R.error(400, "分页参数不能为空");
    }
    if (pageNo < 1) {
      pageNo = 1;
    }
    if (pageSize < 1) {
      pageSize = 10;
    }
    LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
    wrapper.like(StringUtils.hasText(companyName), Company::getCompanyName, companyName);
    wrapper.eq(Company::getIsDelete, 0);
    wrapper.orderByDesc(Company::getCreateTime);
    Page<Company> page = new Page<>(pageNo, pageSize);
    IPage<Company> result = this.page(page, wrapper);
    return R.success(result);
  }

  public R<Company> getOne(Long companyId) {
    if (companyId == null) {
      return R.error(400, "ID不能为空");
    }
    Company company = this.getById(companyId);
    if (company == null) {
      return R.error(404, "未找到该公司");
    }
    return R.success(company);
  }

  public R<String> addCompany(Company company) {
    if (company == null || !StringUtils.hasText(company.getCompanyName())) {
      return R.error(400, "公司名称不能为空");
    }
    // 名称查重
    LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(Company::getCompanyName, company.getCompanyName());
    if (this.count(wrapper) > 0) {
      return R.error(400, "公司名称已存在");
    }
    boolean save = this.save(company);
    return save ? R.success("新增成功") : R.error(500, "新增失败");
  }

  public R<String> deleteCompany(Long companyId) {
    if (companyId == null) {
      return R.error(400, "ID不能为空");
    }
    boolean remove = this.removeById(companyId);
    return remove ? R.success("删除成功") : R.error(500, "删除失败");
  }

  public R<String> updateCompany(Company company) {
    if (company == null
        || company.getId() == null
        || !StringUtils.hasText(company.getCompanyName())) {
      return R.error(400, "ID和公司名称不能为空");
    }
    // 名称查重
    LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
    wrapper
        .eq(Company::getCompanyName, company.getCompanyName())
        .ne(Company::getId, company.getId());
    if (this.count(wrapper) > 0) {
      return R.error(400, "公司名称已存在");
    }
    boolean update = this.updateById(company);
    return update ? R.success("修改成功") : R.error(500, "修改失败");
  }
}
